package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.web.Dao.base.CourierRepository;

@Service
@Transactional
public class CourierServiceimpl implements CourierService {

	@Autowired
	private CourierRepository repository;

	@Override
	@RequiresPermissions("courier_add")   //该权限在搜优人员中  不存在
	public void save(Courier courier) {
		repository.save(courier);
	}

	// 快递员的无条件查询
	@Override
	public Page<Courier> findQuery(Specification<Courier> specification, Pageable pageable) {
		return repository.findAll(specification, pageable);
	}

	// 进行快递员 逻辑删除操作
	@Override
	public void delBatch(String[] idsarray) {
		for (String idStr : idsarray) {
			Integer id = Integer.parseInt(idStr);
			repository.UpdateDeltag(id);
		}

	}

	// 进行快递员围观来查询
	@Override
	public List<Courier> findNoAssociation() {
		// 封装specification
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 查询条件判断列表 为空
				Predicate empty = cb.isEmpty(root.get("fixedAreas").as(Set.class));

				return empty;
			}
		};
		return repository.findAll(specification);
	}

}
